﻿var gift = {
    init: function () {//初始化
        var html = [];
        var o;
        var index = 1;
        for (var i = 0; i < data.list.length; i++) {
            o = data.list[i];
            html.push('<tr class="zhongjiang_table_tr' + index + ' datarow">');
            html.push('<td align="center" valign="middle"><select id="" class="aType"><option value="1" ', o.ATypeID == 1 ? "selected" : "", '>优惠卷</option><option value="2" ', o.ATypeID == 2 ? "selected" : "", '>其它奖品</option></select></td>');
            html.push('<td align="center" valign="middle"><input name="" type="text" class="text_input3 aName" value="', o.AName, '" /></td>');
            html.push('<td align="center" valign="middle"><input name="" type="text" class="text_input4 aAllNum" value="', o.AllNum, '" /></td>');
            html.push('<td align="center" valign="middle"><input name="" type="text" class="text_input5 aPro" value="', o.Probability, '" /></td>');
            html.push('<td align="center" valign="middle"><a href="#" class="btnAdd" onclick="gift.addRow(', index, ',this)">增加奖品</a> &nbsp <a href="#" onclick="gift.remove(this)">删除</a></td>');
            html.push('</tr>');
            index = 3 - index;
        }

        $("#dtList").after(html.join(''));
    },
    addRow: function (index, obj) {//添加新行
        var html = [];
        index = 3 - index;
        html.push('<tr class="zhongjiang_table_tr' + index + ' datarow">');
        html.push('<td align="center" valign="middle"><select id="" class="aType"><option value="1" selected>优惠卷</option><option value="2">其它奖品</option></select></td>');
        html.push('<td align="center" valign="middle"><input name="" type="text" class="text_input3 aName" value="" /></td>');
        html.push('<td align="center" valign="middle"><input name="" type="text" class="text_input4 aAllNum" value="0" /></td>');
        html.push('<td align="center" valign="middle"><input name="" type="text" class="text_input5 aPro" value="0" /></td>');
        html.push('<td align="center" valign="middle"><a href="#" class="btnAdd" onclick="gift.addRow(', index, ',this)">增加奖品</a> &nbsp <a href="#" onclick="gift.remove(this)">删除</a></td>');
        html.push('</tr>');

        $(obj).parents("tr").after(html.join(''));
    },
    remove: function (obj) {//删除本行
        $(obj).parents("tr").remove();
    },
    save: function () {
        var obj = {};
        obj.data = gift.getData();
        if (obj.data) {

            //保存内容
            $.ajax({
                type: "POST",
                url: "handler/AdminHandler.ashx?action=setGift",
                data: obj,
                dataType: 'json',
                success: function (msg) {
                    rel = msg.rel;
                    if (msg.rel >= 0) {
                        alert("操作成功");
                        location.href = "SetRoles.aspx";
                    }
                    else {
                        alert("数据有错，请检查");
                    }
                }
            });
        }
        else {
            alert("有问题请重新操作");
        }
    },
    getData: function () {
        var dt = [];
        var name, num, pro, allnum = 0, sel, selname;
        $(".datarow").each(function (i) {
            sel = $(this).find(".aType").val();
            selname = $(this).find(".aType option:selected").text() == "" && sel == "1" ? "优惠卷" : "其它奖品";
            name = $(this).find(".aName").val();
            num = $(this).find(".aAllNum").val();
            pro = $(this).find(".aPro").val();
            dt.push(sel + "##" + selname + "@@" + name + "@@" + num + "@@" + pro);
            allnum += parseFloat(pro);
        });

        if (allnum != 100) {
            alert("所有奖品的概率之和必须为100；");
            return false;
        }
        return dt.join("~~");
    }
};

$(document).ready(function () {
    $("#btnOK").click(gift.save);
    gift.init();
});
/*
<tr class="zhongjiang_table_tr1">
<td align="center" valign="middle">1</td>
<td align="center" valign="middle"><input name="" type="text" class="text_input3" value="无奖品" /></td>
<td align="center" valign="middle"><input name="" type="text" class="text_input4" value="" /></td>
<td align="center" valign="middle"><input name="" type="text" class="text_input5" value="" /></td>
<td align="center" valign="middle">
<div class="tianjiajiangpin_l fl"><span></span>
<ul>
<li><a href="#">优惠券</a></li>
<li class="tianjiajiangpin_l_di"><a href="#">其他奖品</a></li>
</ul>
</div>
<div class="tianjiajiangpin_r fr"><a class="button_suoding" href="#"></a>
</div>
</td>
</tr>
*/